package com.example.demo.web;

import com.alibaba.fastjson.JSON;
import com.example.demo.mapper.firstsqlserver.GroupPersonMapper;
import com.example.demo.pojo.GroupPerson;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.java2d.pipe.SpanShapeRenderer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class GroupPersonController {

    @Autowired
    private GroupPersonMapper groupPersonMapper;

    @RequestMapping("/listGroupPerson")
    public String listGroupPerson(@RequestParam(value = "start", defaultValue = "1")int start, @RequestParam(value = "size", defaultValue = "5")int size, @RequestParam("cId")String cId, @RequestParam("cName")String cName, Model m) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        List<GroupPerson> pers = groupPersonMapper.listGroupPerson(cId, today);
        List<GroupPerson> charts = groupPersonMapper.getChartData(cId, today);
        int total = pers.size();
        String chartData = JSON.toJSONString(charts);
        PageHelper.startPage(start, size);
        List<GroupPerson> pager = groupPersonMapper.listGroupPerson(cId, today);
        for (GroupPerson person : pager) {
            person.setCreateDate(person.getCreateDate().substring(0, 19));
        }
        PageInfo<GroupPerson> pages = new PageInfo<>(pager);
        String data = JSON.toJSONString(pages.getList());
        m.addAttribute("today", today);
        m.addAttribute("name", cName);
        m.addAttribute("cId", cId);
        m.addAttribute("total", total);
        m.addAttribute("data", data);
        m.addAttribute("chartData", chartData);
        return "listGroupPerson";
    }

    @ResponseBody
    @RequestMapping("/getPageInfo")
    public List<GroupPerson> getPageInfo(@RequestParam(value = "start", defaultValue = "1")int start, @RequestParam(value = "size", defaultValue = "5")int size, @RequestParam("cId")String cId, @RequestParam("date")String date) {
        List<GroupPerson> pers = groupPersonMapper.listGroupPerson(cId, date);
        int total = pers.size();
        PageHelper.startPage(start, size);
        List<GroupPerson> pager = groupPersonMapper.listGroupPerson(cId, date);
        for (GroupPerson person: pager) {
            person.setCreateDate(person.getCreateDate().substring(0, 19));
            person.setTotal(total);
        }
        PageInfo<GroupPerson> pages = new PageInfo<>(pager);
        return pages.getList();
    }

    @ResponseBody
    @RequestMapping("/getChartData")
    public List<GroupPerson> getChartData(@RequestParam("cId")String cId, @RequestParam("date")String date) {
        List<GroupPerson> charts = groupPersonMapper.getChartData(cId, date);
        return charts;
    }
}
